package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.PrivateMessage;
import unipi.database.User;
import unipi.helpers.HTMLPages;
import unipi.helpers.LoginCookie;

public class ViewChat extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	public ViewChat()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	
	}
	
	private String getPage(String uid, String recipient)
	{
		String content = "<h1>Συζήτηση</h1>";
		
		if(uid.equals(recipient))
		{
			content += "Δεν υπάρχει συζήτηση με τον εαυτό σας.";
		}	
		else
		{			
			List<Map<String, String>> conv = PrivateMessage.getConversation(uid, recipient);
			
			if(conv == null)
			{
				content += "Παρουσιάστηκε κάποιο πρόβλημα με την φόρτωση της συζήτησης.";			
			}		
			else
			{	
				if(conv.size() == 0)
				{
					content += "Δεν υπάρχουν μηνύματα στην επιλεγμένη συζήτηση.";				
				}
				else
				{
					Map<String, String> fromData = User.getUserData(uid);
					Map<String, String> toData = User.getUserData(recipient);
					
					content += "<h2 class=\"subtitle\">με " 
								+ toData.get("surname") + " " + toData.get("name") + "</h2>";
					
					String fromPhoto = (fromData == null)?"images/profilePictures/placeholder.png":
										(fromData.get("photo")== null)?"images/profilePictures/placeholder.png":
											fromData.get("photo");
					String toPhoto = (toData == null)?"images/profilePictures/placeholder.png":
										(toData.get("photo")== null)?"images/profilePictures/placeholder.png":
											toData.get("photo");			
					
					content += "<table id=\"leftImgTable\"><tr><td><a href=\"Profile?id=" + uid + "\">" +
								"<img class=\"leftImg\" src=\"" + fromPhoto + "\" alt=\"\"/></a></td></tr></table>";
					content += "<table id=\"rightImgTable\"><tr><td><a href=\"Profile?id=" + recipient + "\">" +
								"<img class=\"rightImg\" src=\"" + toPhoto + "\" alt=\"\"/></a></td></tr></table>";
					
					content += "<div style=\"clear:both\"></div>" +
								"<div class=\"reply\"><a class=\"genericLink\" href=\"SendMessage?id=" + recipient + "\">Απάντηση</a></div>";
					content += "<div id=\"bubbleWrapper\">";
					
					for(Map<String, String> msg : conv)
					{
						String bubble = msg.get("bubble");
						
						content += "<div class=\"" + bubble + "BubbleTop\"></div>";
						content += "<div class=\"" + bubble + "BubbleMiddle\">";
						content += "<div class=\"" + bubble + "BubbleWrapper\">";
						content += msg.get("subject") + ":<br />" + msg.get("body").replaceAll("(\r\n|\n)", "<br />");
						content += "<div class=\"date\">" + msg.get("date") + "</div>";
						content += "</div></div>";	
						content += "<div class=\"" + bubble + "BubbleBottom\"></div>";
						content += "<br /><br /><br />";
					}		
					
					content += "</div><br /><br />";		
				}
			}
		}
		
		return HTMLPages.getGeneralPage(content, uid);	
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		

	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			request.setCharacterEncoding("utf-8");
			String recipient = request.getParameter("id");
			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
			
			out.println(getPage(uid, recipient));
			
			out.close();			
		}
	}
	
}
